package pro.spring.controller.backController;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;




import pro.spring.model.Admin;
import pro.spring.model.Images;

@Controller
@RequestMapping("admins")
public class AdminController extends BaseController{
private String local = "page/back/admin/";
	
	private Integer pagesize = 10 ;

	private String defaultpath="redirect:list.html";
	
	@RequestMapping(value="list.html",method=RequestMethod.GET)
	public String discussList(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,ModelMap map){
		map.put("page",adminService.getPaginationInfo(pagenum, pagesize));
		return local+"alist";
	}
	
	@RequestMapping(value="{id}.html",method=RequestMethod.GET)
	public String delOne(@PathVariable int id){
		adminService.deleteByPrimaryKey(id);
		return defaultpath;
	}
	
	
	@RequestMapping(value="see/{id}.html",method=RequestMethod.GET)
	public String seeOne(@PathVariable int id,ModelMap map){
		map.put("one",adminService.selectByPrimaryKey(id));
		return local+"asee";
	}
	
	@RequestMapping(value="edit/{id}.html",method=RequestMethod.GET)
	public String edit(@PathVariable int id,ModelMap map){
		map.put("one",adminService.selectByPrimaryKey(id));
		return local+"adeit";
	}
	
	
	@RequestMapping(value="change.html",method=RequestMethod.POST)
	public String change(Admin admin){
		adminService.updateByPrimaryKey(admin);
		return "redirect:see/"+admin.getaId()+".html";
	}
	
	@RequestMapping(value="add.html",method=RequestMethod.GET)
	public String addOne(){
		return local+"aadd";
	}
	
	@RequestMapping(value="new.html",method=RequestMethod.POST)
	public String addnew(@RequestParam("file")MultipartFile file,String name,String accord,Integer rank,String password,HttpServletRequest request){
        Integer img = 1;
		if(file!=null){
			String path = request.getSession().getServletContext().getRealPath("page/upload/images");  
	        String fileName = file.getOriginalFilename(); 
	        String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
	        String newName = imagesService.getImageName();
	        File targetFile = new File(path,newName+"."+ext);  
	        if(!targetFile.exists()){  
	            targetFile.mkdirs();
	        } 
	        try {  
	            file.transferTo(targetFile); 
	    		Images im = new Images();
	    		im.setImLoclhost(newName+"."+ext);
	    		im.setImName(name+"的头像");
	    		imagesService.insert(im);
	    		img = imagesService.selectCountNewId();
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } 
        }

		Admin admin = new Admin();
		admin.setaAccount(accord);
		admin.setaName(name);
		admin.setaPassword(password);
		admin.setaRank(rank);
		admin.setImgaesImId(img);
		adminService.insert(admin); 
		return "redirect:see/"+adminService.selectByNew()+".html";
	}
	
	@RequestMapping(value="otherconfig.html",method=RequestMethod.GET)
	public String otherconfig(ModelMap map){
		String vediotoken;
		try{
			vediotoken = adminService.selectByNameForVedio("vediotoken").getaPassword();
		}catch(Exception e){
			vediotoken="当前没有设置！";
		}
		map.put("vediotoken",vediotoken);
		return local+"otherconfig";
	}
	
}
